package com.wy.jfree_chart;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class LineChartDemo extends JFrame {

    public LineChartDemo(String title) {
        super(title);
        
        // 创建数据集
        XYDataset dataset = createDataset();
        
        // 创建图表
        JFreeChart chart = createChart(dataset);
        
        
        // 创建面板并添加到窗口
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(800, 600));
        setContentPane(chartPanel);
    }

    private XYDataset createDataset() {
        // 创建数据系列
        XYSeries series1 = new XYSeries("产品A");
        XYSeries series2 = new XYSeries("产品B");
        
        Random random = new Random();
        double value1 = 100;
        double value2 = 80;
        
        // 生成随机数据点
        for (int i = 1; i <= 12; i++) {
            value1 = value1 + (random.nextDouble() - 0.5) * 20;
            value2 = value2 + (random.nextDouble() - 0.5) * 15;
            
            series1.add(i, value1);
            series2.add(i, value2);
        }
        
        // 将系列添加到数据集
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series1);
        dataset.addSeries(series2);
        
        return dataset;
    }

    private JFreeChart createChart(XYDataset dataset) {
        // 创建折线图
        JFreeChart chart = ChartFactory.createXYLineChart(
                "月度销售趋势",           // 图表标题
                "月份",                  // X轴标签
                "销售额(万元)",          // Y轴标签
                dataset,                // 数据集
                PlotOrientation.VERTICAL, // 图表方向
                true,                   // 是否显示图例
                true,                   // 是否生成工具提示
                false                   // 是否生成URL链接
        );

        // 自定义图表外观
        XYPlot plot = chart.getXYPlot();
        
        // 设置背景颜色
        plot.setBackgroundPaint(Color.WHITE);
        
        // 设置网格线
        plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
        plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
        
        // 设置线条渲染器
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesPaint(0, Color.BLUE);
        renderer.setSeriesPaint(1, Color.RED);
        renderer.setSeriesStroke(0, new BasicStroke(2.0f));
        renderer.setSeriesStroke(1, new BasicStroke(2.0f));
        renderer.setSeriesShapesVisible(0, true);
        renderer.setSeriesShapesVisible(1, true);
        
        plot.setRenderer(renderer);
        
        return chart;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            LineChartDemo demo = new LineChartDemo("JFreeChart 折线图示例");
            demo.setSize(800, 600);
            demo.setLocationRelativeTo(null);
            demo.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            demo.setVisible(true);
        });
    }
}